<?php
defined ( 'BASEPATH' ) or exit ( 'No direct script access allowed' );
class Category extends CI_Controller {
	public function __construct() {
		parent::__construct ();
		$this->admin->isLogin ( site_url ( 'login' ) );
		$this->load->model('category_model');
	}
	public function index($page=1){
		$whereArr = array('status'=>1);
		$count = $this->category_model->count ( $whereArr );
		$params = array(
				"url_template" => site_url ( 'Category/index/{page}' ),
				"page_count" => $count,
				"page_now" => $page,
				"page_size" => 10
		);
		$params["total_pages"] = ceil ( $params['page_count'] / $params['page_size'] );
		$this->load->library ( 'paging', $params );
		$category_list = $this->category_model->result ( $whereArr, $page, $params['page_size'] );
		$data['category_list'] = $category_list;
		$this->load->view("category_index",$data);
	}
	/*
	 *添加栏目
	 */
	public function add(){
		$category_list = $this->findCategory();
		$data['category_list'] = $category_list;
		$this->load->view("category_add",$data);
	}
	/*
	 *添加操作
	 */
	public function doadd(){
		$data['pcid'] = $this->input->post("pcid");
		$data['cname'] = $this->input->post("cname");
		$result = $this->category_model->insert($data);
		if($result){
			//echo "添加栏目成功!!";
			echo "<script type='text/javascript'>alert('添加栏目成功!!');window.location.href='".site_url('Category')."'</script>";
		}else{
			//echo "添加栏目失败!!";
			echo "<script type='text/javascript'>alert('添加栏目失败!!');window.location.href='".site_url('Category/add')."'</script>";
		}
	}
	public function findCategory(){
		$whereArr = array("status"=>1);
		$result_list = $this->category_model->resultAll($whereArr);
		return $result_list;
	}
	/*
	 *删除栏目
	 */
	public function del($cid=1){
		$whereArr = array("cid"=>$cid);
		$dataArr = array("status"=>0);
		$result = $this->category_model->update($dataArr,$whereArr);
		if($result){
			$data['status'] = 1;
			$data['info'] = "栏目删除成功!";
		}else{
			$data['status'] = 0;
			$data['info'] = "栏目删除失败!";
		}
		echo json_encode($data);
	}
	/*
	 *删除多个栏目
	 */
	public function duodel(){
		$ids = $this->input->post('ids');
		if(empty($ids))
		{
			$flag =array('status'=>-1,'info'=>'请选择数据!!');
		}else{
			$dataArr=array("status"=>0);
			$result=$this->category_model->update_in($dataArr,'cid',$ids);
			if($result){
				$flag = array('status'=>1,'info'=>'栏目删除成功!');
			}else{
				$flag = array('status'=>0,'info'=>'栏目删除失败!');
			}
		}
		echo json_encode($flag);
	}
	/*
	 *加载编辑页面 
	 */
	public function edit($cid=1){
		$whereArr = array("cid"=>$cid);
		$category = $this->category_model->row($whereArr);
		$category_list = $this->findCategory();
		$data['category'] = $category;
		$data['category_list'] = $category_list;
		$this->load->view("category_edit",$data);
	}
	/*
	 *提交编辑
	 */
	public function doedit(){
		$cid = $this->input->post("cid");
		$whereArr = array('cid'=>$cid);
		$dataArr['pcid'] = $this->input->post("pcid");
		$dataArr['cname'] = $this->input->post("cname");
		$result = $this->category_model->update($dataArr,$whereArr);
		if($result){
			echo "栏目修改成功!!";
		}else{
			echo "栏目修改失败!!";
		}
	}
}